package com.yunzainfo.demo.mq.jms;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yunzainfo.demo.mq.entity.BaseUsers;
import com.yunzainfo.demo.mq.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Slf4j
@Component
public class Receiver {
    @Resource
    private UserService userService;

    @JmsListener(destination = "addUser")
    public void receivedMessage1(String message) throws IOException {
        log.info("addUser接收到：" + message);
        ObjectMapper objectMapper = new ObjectMapper();
        BaseUsers baseUsers = objectMapper.readValue(message, BaseUsers.class);
        userService.addUser(baseUsers);
    }

    @JmsListener(destination = "updateUser")
    public void receivedMessage2(String message) throws IOException {
        log.info("updateUser接收到：" + message);
        ObjectMapper objectMapper = new ObjectMapper();
        BaseUsers baseUsers = objectMapper.readValue(message, BaseUsers.class);
        userService.updateUser(baseUsers);
    }

    @JmsListener(destination = "deleteUser")
    public void receivedMessage3(String message) throws IOException {
        log.info("deleteUser接收到：" + message);
        ObjectMapper objectMapper = new ObjectMapper();
        BaseUsers baseUsers = objectMapper.readValue(message, BaseUsers.class);
        userService.deleteUser(baseUsers);
    }
}
